<?php
	$controller = $this->params['controller'];
	$action = $this->params['action'];
	
	$sort_by = (!empty($this->params['named']['sort_by'])) ? $this->params['named']['sort_by'] : '';
	
	$name_sort_url = array('controller' => $controller, 'action' => $action, 'sort_by' => ($sort_by == 'name_desc') ? 'name_asc' : 'name_desc');
	$creator_sort_url = array('controller' => $controller, 'action' => $action, 'sort_by' => ($sort_by == 'creator_desc') ? 'creator_asc' : 'creator_desc');
	$modified_sort_url = array('controller' => $controller, 'action' => $action, 'sort_by' => ($sort_by == 'modified_desc') ? 'modified_asc' : 'modified_desc');
	
?>
<div class="window documents">
	<div class="title">Documents
		<?php if (!empty($current_project) && $current_user['User']['role'] != 'spectator') : ?>
			- <a href="/documents/add">New document</a>
		<?php endif; ?>
	</div>
	<div class="content">
		<?php if (empty($current_project)) : ?>
			<table style="width: 100%;">
				<tr><td>Select the project in the left panel</td></tr>
			</table>
		<?php else : ?>
			<table style="width: 100%;">
				<tr class="header">
					<td class="name <?php if (preg_match('/name/', $sort_by)) {echo "sort_by";} ?>">
						<a href="<?php echo $html->url($name_sort_url); ?>"><div>Document name</div></a>
					</td>
					<td class="creator <?php if (preg_match('/creator/', $sort_by)) {echo "sort_by";} ?>">
						<a href="<?php echo $html->url($creator_sort_url); ?>"><div>Creator</div></a>
					</td>
					<td class="modified <?php if (preg_match('/modified/', $sort_by)) {echo "sort_by";} ?>">
						<a href="<?php echo $html->url($modified_sort_url); ?>"><div>Update</div></a>
					</td>
					<td class="actions">Action</td>
				</tr>
			<?php foreach($documents as $i => $document) : ?>
				<tr class="document-item">
					<td class="name <?php echo ($i % 2 == 0) ? 'not-even' : 'even'; ?>">
						<a href="<?php echo $html->url( array('controller' => 'documents', 'action' => 'view', $document['Document']['id'])); ?>">
							<div><?php echo $document['Document']['name']; ?></div>
						</a>
					</td>
					<td class="creator <?php echo ($i % 2 == 0) ? 'not-even' : 'even'; ?>"><?php echo $document['Creator']['username']; ?></td>
					<td class="modified <?php echo ($i % 2 == 0) ? 'not-even' : 'even'; ?>"><?php echo $document['Document']['modified']; ?></td>
					<td class="actions <?php echo ($i % 2 == 0) ? 'not-even' : 'even'; ?>">
						<a href="<?php echo $html->url( array('controller' => 'documents', 'action' => 'view', $document['Document']['id'])); ?>">view</a>
						<?php if ($current_user['User']['role'] != 'spectator') : ?>
							<?php if ($document['Document']['blocked'] == 0 || $document['Document']['blocked'] == $current_user['User']['id']) : ?>
							<a href="<?php echo $html->url( array('controller' => 'documents', 'action' => 'edit', $document['Document']['id'])); ?>">edit</a>
							<a onclick="return confirm('You really want to delete a document?');" href="<?php echo $html->url( array('controller' => 'documents', 'action' => 'delete', $document['Document']['id'])); ?>">delete</a>
							<?php endif; ?>
						<?php endif; ?>
					</td>
				</tr>
			<?php endforeach; ?>
			</table>
		<?php endif; ?>
	</div>
</div>